package com.example.blood_sugar.activities;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import com.example.blood_sugar.R;
import com.example.blood_sugar.utils.HandleBackUtil;

/**
 * 自定义的基础Activity，抽取了Activity托管Fragment时的重复代码
 */
public abstract class SingleFragmentActivity extends FragmentActivity {
    protected abstract Fragment createFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        // 禁止横屏，不禁止太多坑了
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragment_container);
        if (fragment == null) {
            fragment =  createFragment();
            fm.beginTransaction()
                    .add(R.id.fragment_container, fragment)
                    .commit();
        }
    }
/*
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity);
    }*/

    /**
     * 重写返回键点击方法，让其执行自定义的方法（将返回事件分发给所有继承了BaseFragment的fragment）
     */
    @Override
    public void onBackPressed() {
        if (!HandleBackUtil.handleBackPress(this)) {
//            overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity);
            super.onBackPressed();
        }
    }

}
